var http = require( '../../utils/util' )
import { restUserServerOrder, restUserServerOrderCancel } from '../../api/order.js'
var app = getApp()

Page({
  data: {
      winWidth: 0,
      winHeight: 0,
      currentTab: 0,
	  
	  status: 1,// 状态 1已预约 2已完成 3已取消
	  list:[],
	  page: 1,
	  pageSize: 12,
	  listFlag: true
  },
  onLoad: function() {

      var that = this;

      /**
       * 获取当前设备的宽高
       */
      wx.getSystemInfo( {
          success: function( res ) {
              that.setData( {
                  winWidth: res.windowWidth,
                  winHeight: res.windowHeight
              });
          }
      });
	  
	  this.restUserServerOrder();
	  
  },
    
//  tab切换逻辑
  swichNav: function( e ) {

      var that = this;

      if( this.data.currentTab === e.target.dataset.current ) {
          return false;
      } else {
		  console.log(e.target.dataset.current);
          that.setData( {
              currentTab: e.target.dataset.current,
			  list: [],
			  page: 1,
			  status: parseInt(e.target.dataset.current)+1
          });
		  that.restUserServerOrder();
      }
  },

  bindChange: function( e ) {

      var that = this;
      that.setData( { currentTab: e.detail.current });

  },
  
  showOrderDetail: function (e) {
	  console.log(e);
	  wx.navigateTo({
		  url: '/pages/order/detail?id='+e.currentTarget.dataset.orderid
	  });
  },
  
  restUserServerOrder: function() {
	  var _that = this;
	  var data = {
		  status: this.data.status,
		  userId: app.globalData.wxlogin.userId,
		  page: this.data.page,
		  pageSize: this.data.pageSize
	  };
	  restUserServerOrder(data).then( res => {
		  console.log(res)
		  if(res.code == 0){
			  _that.setData({
				  list: res.data,
				  page: _that.data.page+1
			  })
		  }
		  
	  });
  },
  
  candelete: function(e) {
	  var _that = this;
	  console.log(e);
	  var targetindx = e.currentTarget.dataset.indx;
	  var data = {
			userId: app.globalData.wxlogin.userId,
	  		  id: _that.data.list[targetindx].id
	  };
	  restUserServerOrderCancel(data).then( res => {
	  		  console.log(res)
	  		  if(res.code == 0){
	  			var oldlist = _that.data.list;
	  			var list = oldlist.splice(targetindx,1);
	  			_that.setData({list: oldlist});
	  		  }else{
						wx.showModal({title:"提示",content: res.msg});
					}
	  		  
	  });
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  	var _that = this;
  	wx.showNavigationBarLoading(); //在标题栏中显示加载图标
  	_that.setData({
  		listFlag: true,
  		page: 1,
  		list: []
  	});
  	_that.restUserServerOrder();
  	setTimeout(function(){
  		wx.hideNavigationBarLoading(); //完成停止加载图标
  	}, 1111);
  	
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  	this.restUserServerOrder();
  },
  
})